﻿Imports System.Data.SqlClient

Public Class frmRenameAnAccount

    Private Sub txtAccNo_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtAccNo.KeyUp
        If e.KeyCode = Keys.Enter Then
            Me.txtAccName.Text = GetAccName(Me.txtAccNo.Text.Trim)
            Me.txtNewName.Text = GetAccName(Me.txtAccNo.Text.Trim)
            Me.txtNewName.SelectAll()
        End If
    End Sub

    Private Sub txtAccNo_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtAccNo.TextChanged
        Me.txtAccName.Clear()
    End Sub

    Private Sub btnSearchAccNo_Click(sender As System.Object, e As System.EventArgs) Handles btnSearchAccNo.Click
        Dim a As New frmSelectAccount
        a.ShowDialog()

        If SelAccNo <> "" Then
            Me.txtAccNo.Text = SelAccNo
            Me.txtAccName.Text = SelAccName
            Me.txtNewName.Text = SelAccName
            Me.txtNewName.SelectAll()
        End If
    End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        Me.ErrProv.Clear()
        If Me.txtAccName.Text.Trim.Length = 0 Then
            Me.ErrProv.SetError(Me.txtAccNo, "Please fill in all details")
            Me.txtAccNo.Focus()

        ElseIf Me.txtAccName.Text.Trim.Length = 0 Then
            Me.ErrProv.SetError(Me.txtAccNo, "Please fill in all details")
            Me.txtAccNo.Focus()

        ElseIf Me.txtNewName.Text.Trim.Length = 0 Then
            Me.ErrProv.SetError(Me.txtNewName, "Please fill in all details")
            Me.txtNewName.Focus()
        Else
            Try
                Me.Cursor = Cursors.WaitCursor

                Dim cmd As New SqlCommand
                Dim Trans As SqlTransaction

                cnn.Open()
                Trans = cnn.BeginTransaction
                cmd.Transaction = Trans
                cmd.Connection = cnn

                '1) Rename in Chart of Accounts
                cmd.CommandText = "Update Acc Set Acc4=@Acc4 Where AccNo=@AccNo"
                cmd.Parameters.Clear()
                cmd.Parameters.AddWithValue("@AccNo", Me.txtAccNo.Text.Trim)
                cmd.Parameters.AddWithValue("@Acc4", Me.txtNewName.Text.Trim)
                cmd.ExecuteNonQuery()

                '2) Rename in Temp Transactions
                cmd.CommandText = "Update TempTransactions Set Acc4=@Acc4 Where AccNo=@AccNo"
                cmd.Parameters.Clear()
                cmd.Parameters.AddWithValue("@AccNo", Me.txtAccNo.Text.Trim)
                cmd.Parameters.AddWithValue("@Acc4", Me.txtNewName.Text.Trim)
                cmd.ExecuteNonQuery()

                '3) Rename in Transactions
                cmd.CommandText = "Update Transactions Set Acc4=@Acc4 Where AccNo=@AccNo"
                cmd.Parameters.Clear()
                cmd.Parameters.AddWithValue("@AccNo", Me.txtAccNo.Text.Trim)
                cmd.Parameters.AddWithValue("@Acc4", Me.txtNewName.Text.Trim)
                cmd.ExecuteNonQuery()

                Trans.Commit()
                cnn.Close()

                MsgBox("Saved Successfully")

                Me.txtAccName.Text = GetAccName(Me.txtAccNo.Text.Trim)
                Me.txtNewName.Text = GetAccName(Me.txtAccNo.Text.Trim)

                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub
End Class